import 'package:flutter/material.dart';
import 'package:robotcontrol/page/home_page.dart';
import 'package:robotcontrol/page/discovery/discovery_page.dart';
import 'package:robotcontrol/utils/toast.dart';
import 'package:robotcontrol/utils/tip_util.dart';

class HomeTabNavigation extends StatefulWidget {
  @override
  _TavNavigationState createState() => _TavNavigationState();
}

class _TavNavigationState extends State {
  DateTime lastTime = DateTime.now(); //点击计算时间
  Widget _currentBody = Image.network( "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.jj20.com%2Fup%2Fallimg%2F1114%2F113020142315%2F201130142315-1-1200.jpg&refer=http%3A%2F%2Fimg.jj20.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1657336678&t=e4ed19f2f125208ee49f8858ad654b7a",
      fit: BoxFit.fill); //
  int _currentIndex = 0;

  @override
  Widget build(BuildContext context) {
    //防止用户误触返回按钮
    return WillPopScope(
        child: Scaffold(
          body: PageView(
            controller: _pageController,
            // physics: NeverScrollableScrollPhysics(),
            onPageChanged: (index){
              setState((){
                _currentIndex = index;
              });
            },
            children: [
              HomePage(),
              DiscoveryPage(),
          Image.network("https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.jj20.com%2Fup%2Fallimg%2F4k%2Fs%2F02%2F2109242306111155-0-lp.jpg&refer=http%3A%2F%2Fimg.jj20.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1657336678&t=574b1b961d6fd22da87458af6a64354b",
              fit: BoxFit.fill),
          Image.network("https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.jj20.com%2Fup%2Fallimg%2F1114%2F063021120F9%2F210630120F9-1-1200.jpg&refer=http%3A%2F%2Fimg.jj20.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1657336678&t=45eeb0dbbb08b0a70c120237e42a6c09",
              fit: BoxFit.fill)
          ],
          ),
          bottomNavigationBar: BottomNavigationBar(
            items: _item(),
            currentIndex: _currentIndex,
            type: BottomNavigationBarType.fixed,
            selectedItemColor: Color(0xff000000),
            unselectedItemColor: Color(0xff9a9a9a),
            // onTap: _onTap,
            onTap: (index){
              _pageController.jumpToPage(index);
              setState((){
                _currentIndex = index;
              });
            },
          ),
        ),
        onWillPop: _onWillPop);
  }
  var _pageController = PageController();
  List<BottomNavigationBarItem> _item() {
    return [
      _bottomItem(TipUtil.home, 'images/ic_home_normal.png',
          'images/ic_home_selected.png'),
      _bottomItem(TipUtil.discovery, 'images/ic_discovery_normal.png',
          'images/ic_discovery_selected.png'),
      _bottomItem(TipUtil.hot, 'images/ic_hot_normal.png',
          'images/ic_hot_selected.png'),
      _bottomItem(TipUtil.mine, 'images/ic_mine_normal.png',
          'images/ic_mine_selected.png')
    ];
  }

  _bottomItem(String title, String normalIcon, String selectIcon) {
    return BottomNavigationBarItem(
        icon: Image.asset(
          normalIcon,
          width: 24,
          height: 24,
        ),
        activeIcon: Image.asset(selectIcon, width: 24, height: 24),
        label: title);
  }

  // void _onTap(int index) {
  //   switch (index) {
  //     case 0:
  //       _currentBody = Image.network(
  //           fit: BoxFit.fill,
  //           "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.jj20.com%2Fup%2Fallimg%2F1114%2F113020142315%2F201130142315-1-1200.jpg&refer=http%3A%2F%2Fimg.jj20.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1657336678&t=e4ed19f2f125208ee49f8858ad654b7a");
  //       break;
  //     case 1:
  //       _currentBody = Image.network(
  //           fit: BoxFit.fill,
  //           "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.jj20.com%2Fup%2Fallimg%2Ftp09%2F21061109241GT3-0-lp.jpg&refer=http%3A%2F%2Fimg.jj20.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1657336678&t=8a68f10b93825b34c87ca62cd6742fc5");
  //       break;
  //     case 2:
  //       _currentBody = Image.network(
  //           fit: BoxFit.fill,
  //           "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.jj20.com%2Fup%2Fallimg%2F4k%2Fs%2F02%2F2109242306111155-0-lp.jpg&refer=http%3A%2F%2Fimg.jj20.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1657336678&t=574b1b961d6fd22da87458af6a64354b");
  //       break;
  //     case 3:
  //       _currentBody = Image.network(
  //           fit: BoxFit.fill,
  //           "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.jj20.com%2Fup%2Fallimg%2F1114%2F063021120F9%2F210630120F9-1-1200.jpg&refer=http%3A%2F%2Fimg.jj20.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1657336678&t=45eeb0dbbb08b0a70c120237e42a6c09");
  //       break;
  //   }
  //   setState(() {
  //     _currentIndex = index;
  //   });
  // }

  Future<bool> _onWillPop() async {
    if (null == lastTime ||
        DateTime.now().difference(lastTime) > Duration(seconds: 2)) {
      lastTime = DateTime.now();
      ToastUtil.toast(TipUtil.exit_tip);
      return false;
    }
    return true;
  }
}
